package com.pi.pro.xcmg.am.rf.web.cust.controller;


import com.fasterxml.jackson.annotation.JsonView;
import com.pi.common.utils.StringUtils;
import com.pi.framework.core.bean.PageResult;
import com.pi.framework.core.bean.Result;
import com.pi.framework.core.service.ICrudService;
import com.pi.framework.core.util.SecurityUtils;
import com.pi.pro.xcmg.am.common.XcmgAmConstants;
import com.pi.pro.xcmg.am.common.vo.act.PalletizeChangeData;
import com.pi.pro.xcmg.am.common.vo.edi.XwmsInventoryLineDto;
import com.pi.pro.xcmg.am.rf.fac.service.XcmgAmRfCustomWorkService;
import com.pi.pro.xcmg.am.was.edi.infor.common.InforWasExchangeSupport;
import com.pi.pro.xcmg.am.was.edi.infor.common.InforWasXcmgExchangeConstants;
import com.pi.pro.xcmg.am.was.edi.infor.dto.req.StorageQueryData;
import com.pi.pro.xcmg.am.was.fac.service.InforCommonWorkService;
import com.pi.pro.xcmg.am.was.fac.service.WasCustomWorkService;
import com.pi.wms.dto.act.TaskActionDto;
import com.pi.wms.entity.TwtActPalletizeList;
import com.pi.wms.rf.api.controller.BaseApiController;
import com.pi.wms.service.PalletizeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Slf4j
@Api(value = "码盘管理")
@RestController
@RequestMapping("/inbound/palletize/ca")
public class CustApplyPalletizeApiController extends BaseApiController<TwtActPalletizeList, Long> {

    @Autowired
    private PalletizeService palletizeService;

    @Autowired
    private XcmgAmRfCustomWorkService rfCustomWorkService;

    @Autowired
    private WasCustomWorkService wasCustomWorkService;

    @Autowired
    private InforCommonWorkService inforCommonWorkService;
	
	@Override
	protected ICrudService<TwtActPalletizeList, Long> getCrudService() {
		return this.palletizeService;
	}


    @JsonView(value = { PageResult.View.class })
    @ApiOperation("查询XWMS暂存区库存以用于码盘")
    @GetMapping("/xwmsinventory")
    public Result queryXwmsInventory(@ApiParam(name = "materialcode")  @RequestParam(name = "materialcode", required=true) String materialcode,
                                     @ApiParam(name = "locationcode")  @RequestParam(name = "locationcode", required=false) String locationcode,
                                     @ApiParam(name = "warehousecode") @RequestParam(name = "warehousecode", required=false) String warehousecode,
                                     @ApiParam(name = "usercode") @RequestParam(name = "usercode", required=false) String usercode) {

        log.debug("Controller.PalletizeApiController query xwms inventoty()//materialcode=%s, loccode=%s, warehousecode=%s,  usercode=%s", materialcode, locationcode, warehousecode,  usercode);
        if (StringUtils.isNullOrBlank(materialcode)){
            return resultError(501, "请指定物料编码");
        }
        //
        StorageQueryData params = InforWasExchangeSupport.newQuery(InforWasXcmgExchangeConstants.INFOR_MAP_WH101_WAREHOUSER_ID, materialcode, "");
        TaskActionDto actionDto =new TaskActionDto();
        actionDto.setDivisionCode(warehousecode);
        List<XwmsInventoryLineDto> lines =  this.inforCommonWorkService.translateTo(warehousecode, this.inforCommonWorkService.queryAndGroupInventory(params, actionDto));
        if (!StringUtils.isNullOrBlank(actionDto.getErrorText())){
            return resultError(501, "获取数据失败:"+actionDto.getErrorText());
        }
        this.wasCustomWorkService.calculateInventoyDataPalletizeQuantity(lines, false);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("header", this.inforCommonWorkService.translateTo(actionDto));
        map.put("lines", lines);
        return resultOk("获取数据成功", Arrays.asList(map));
    }


    @JsonView(value = { PageResult.View.class })
    @ApiOperation("查询指定托盘码码盘汇总")
    @GetMapping("/query")
    public Result query(@ApiParam(name = "palletnumber") @RequestParam(name = "palletnumber", required=true) String palletnumber,
                        @ApiParam(name = "materialcode") @RequestParam(name = "materialcode", required = false) String materialcode,
                        @ApiParam(name = "tracenumber") @RequestParam(name = "tracenumber", required=false) String tracenumber,
                        @ApiParam(name = "docnumber") @RequestParam(name = "docnumber", required=false) String docnumber,
                        @ApiParam(name = "linenumber") @RequestParam(name = "linenumber", required=false) String linenumber,
                        @ApiParam(name = "materialnumber") @RequestParam(name = "materialnumber", required=false) String materialnumber) {

        if (StringUtils.isNullOrBlank(palletnumber)){
            return resultError(501, "请指定查询条件:托盘码");
        }
        //AsnHeaderDto header=this.translateTo(h);
        //List<AsnLineDto> lines=this.translateTo(h,ds);
        Map<String, Object> map = new HashMap<String, Object>();
        //map.put("header", header);
        //map.put("lines", lines);
        return resultOk("查询码盘明细成功", Arrays.asList(map));
    }


    @JsonView(value = { PageResult.View.class })
    @ApiOperation("保存码盘数据")
    @GetMapping("/save")
    public Result save(@ApiParam(name = "changedata") @ModelAttribute  PalletizeChangeData changedata) {
    //public Result save(@ApiParam(name = "changedata") @RequestParam(name = "changedata", required=false) PalletizeChangeData changedata) {

        log.debug("Controller.PalletizeApiController/V2 save() //changedata={}", changedata);

        String msg="";
        if (changedata==null){
            msg="请指定码盘数据.";
            return resultError(501, msg);
        }
        if (StringUtils.isNullOrBlank(changedata.getContainnumber())){
            msg="请扫描托盘码.";
            return resultError(501, msg);
        }
        if (StringUtils.isNullOrBlank(changedata.getMaterialcode())){
            msg="请扫描物料码.";
            return resultError(501, msg);
        }
        if (StringUtils.isNullOrBlank(changedata.getDivisioncode())){
            changedata.setDivisioncode(XcmgAmConstants.DIVISION_WH101_CODE);
        }
        if (StringUtils.isNullOrBlank(changedata.getUsercode())){
            changedata.setUsercode(SecurityUtils.getLoginUsername());
        }

        TaskActionDto actionDto=new TaskActionDto();
        actionDto.setDivisionCode(changedata.getDivisioncode());
        actionDto.setWorkMode(changedata.getWorkmode());
        actionDto.setWorker(changedata.getUsercode());
        msg = this.wasCustomWorkService.palletizeCreate(changedata, actionDto);
        if (!StringUtils.isNullOrBlank(msg)){
            return resultError(501, "码盘收货失败:"+msg);
        }
        return resultOk("码盘收货成功");
    }

    @JsonView(value = { PageResult.View.class })
    @ApiOperation("删除指定的码盘明细")
    @GetMapping("/delete")
    public Result delete(@ApiParam(name = "id") @RequestParam(name = "id", required=true) String id,
                         @ApiParam(name = "palletnumber") @RequestParam(name = "palletnumber", required=false) String palletnumber,
                         @ApiParam(name = "tracenumber") @RequestParam(name = "tracenumber", required=false) String tracenumber,
                         @ApiParam(name = "usercode") @RequestParam(name = "usercode", required=false) String usercode) {


        log.debug("Controller.PalletizeApiController delete()//Id={}, PalNum={}, TraceNum={}, usercode={}", String.valueOf(id), palletnumber, tracenumber, usercode);

        Long palletid=StringUtils.getLongValue(id);
        String msg = this.rfCustomWorkService.palletizeDelete(palletid, palletnumber, tracenumber, usercode);

        if (!StringUtils.isNullOrBlank(msg)){
            return resultError(501, "删除码盘失败:"+msg);
        }
        return resultOk("删除码盘成功");
    }


    @JsonView(value = { PageResult.View.class })
    @ApiOperation("清除指定托盘的码盘明细")
    @GetMapping("/clean")
    public Result clean(@ApiParam(name = "palletnumber") @RequestParam(name = "palletnumber", required=true) String palletnumber,
                        @ApiParam(name = "usercode") @RequestParam(name = "usercode", required=false) String usercode) {

        log.debug("Controller.PalletizeApiController clean()//PalNum=%s, usercode=%s", palletnumber, usercode);
        if (StringUtils.isNullOrBlank(palletnumber)){
            return resultError(501, "请指定托盘码");
        }
        String msg=this.rfCustomWorkService.palletizeClean(palletnumber, "", usercode);
        if (!StringUtils.isNullOrBlank(msg)){
            return resultError(501, "清除码盘失败:"+msg);
        }
        return resultOk("清除码盘成功");
    }




}
